﻿var indicationUpdateVM = {
    IndicationID: ko.observable(),
    PatientID: ko.observable(),
    PatientName: ko.observable(),
    DoctorID: ko.observable(),
    DoctorName: ko.observable(),
    CreateDate: ko.observable(),
    IndicationInformation: ko.observable(),
    IndicationResult: ko.observable(),
    ImageVisitID: ko.observable(),
    ImageID: ko.observable(),
    initialData: function () {
        var self = this;
        self.IndicationID('...');
        self.PatientID('...');
        self.PatientName('...');
        self.DoctorID('...');
        self.DoctorName('...');
        self.CreateDate('...');
        self.IndicationInformation('...');
        self.IndicationResult('...');
        self.ImageVisitID('...');
        self.ImageID('...');
    },
    loadRequest: null,
    cancelLoadRequest: function () {
        var self = this;
        if (self.loadRequest != null) {
            self.loadRequest.abort();
        }
    },
    loadIndicationInfo: function (id, imageController, indexController) {
        var self = this;
        //clear old data
        self.initialData();
        //reset, clear old data, construct, disable event handling
        imageController.clearOldData();
        indexController.message.processing("loading");
        //stop handling
        indexController.enableUpdateIndicationTool(false);
        indexController.enableBtnUpdateIndicationSave(false);
        //Ajax call
        self.loadRequest = $.ajax({
            type: 'post',
            url: home + 'Radiologist/GetIndication',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ indicationID: id }),
            success: function (data) {
                //alert(JSON.stringify(data));
                self.IndicationID(data[0].IndicationID);
                self.PatientID(data[0].PatientID);
                self.PatientName(data[0].PatientName);
                self.DoctorID(data[0].DoctorID);
                self.DoctorName(data[0].DoctorName);
                self.CreateDate(data[0].CreateDate);
                self.IndicationInformation(data[0].IndicationInformation);
                self.IndicationResult(data[0].IndicationResult);
                self.ImageVisitID(data[0].ImageVisitID);
                self.ImageID(data[0].ImageID);
                indexController.message.processComplete("load_complete");
                //load images
                if (self.ImageVisitID() != null) {
                    imageController.loadImages(self.PatientID(), self.ImageVisitID(), self.ImageID());
                }
                //start handling
                indexController.enableUpdateIndicationTool(true);
                indexController.enableBtnUpdateIndicationSave(true);
            },
            error: function (err) {
                if (err.status == 200) {
                    messageController.processComplete(err.responseText);
                }
                else {
                    messageController.processComplete("load_error");
                }
            }
        });
    },
    saveIndication: function (indexController) {
        indexController.message.processing("saving");
        indexController.enableBtnUpdateIndicationSave(false);
        indexController.enableBtnUpdateIndicationCancel(false);
        $.ajax({
            type: 'post',
            url: home + 'Radiologist/SaveIndication',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: ko.toJSON(this),
            success: function (data) {
                //close update indication form
                indexController.displayUpdateIndication(false);
                //remove old data, add new data to list of indications
                patientVM_Radiologist.Indications.remove(function (item) { return item.IndicationID == data[0].IndicationID });
                patientVM_Radiologist.Indications.unshift(data[0]);
                patientVM_Radiologist.sortIndicationsDESC();
                indexController.message.processComplete("save_success");
            },
            error: function (err) {
                if (err.status == 200) {
                    messageController.processComplete(err.responseText);
                }
                else {
                    messageController.processComplete("save_fail");
                }
            },
            complete: function (xhr, textStatus) {
                indexController.enableBtnUpdateIndicationSave(true);
                indexController.enableBtnUpdateIndicationCancel(true);
            }
        });
    }
};